<?php
class CasXML extends DomDocument
{
    function __construct($version = null, $encoding = null) {
        parent::__construct($version, $encoding);
    }

    public function createElement($tagName, $value=null) {
        $node=parent::createElement($tagName);
        if (!is_null($value)) {
            $node->appendChild(
                $this->createTextNode(utf8_encode($value))
            );
        }
        return $node;
    }

    public function createElementNS($namespaceURI, $qualifiedName, $value=null) {
        $node=parent::createElementNS($namespaceURI, $qualifiedName);
        if (!is_null($value)) {
            $node->appendChild(
                $this->createTextNode(utf8_encode($value))
            );
        }
        return $node;
    }

    public function addItem( $name, $folder, $description,
                            $sentence, $remarksentence, $remarkexample,
                            $relatedcommand,$template,$remarktemplate,
                            $exercisedescription,$hint,$solution,$filehelp, $topico = "nada", $subtopico = '' )
    {
        $item = $folder->appendChild( $this->createElement("item") );
        $item->setAttribute("name", $name);
        $casxml_description = $item->appendChild( $this->createElement("description",$description) );
        $casxml_examples = $item->appendChild( $this->createElement("examples") );
        $casxml_example = $casxml_examples->appendChild( $this->createElement("example") );

        $comandos = explode("\r\n",$sentence);
        foreach ($comandos as $c)
        {
            $casxml_commandline = $casxml_example->appendChild($this->createElement("commandline"));
                $casxml_sentence = $casxml_commandline->appendChild($this->createElement("sentence",$c));
                $casxml_remark = $casxml_commandline->appendChild($this->createElement("remark"));
        }
        $casxml_remarkex = $casxml_example->appendChild($this->createElement("remark",$remarkexample));

        $casxml_relatecommand = $item->appendChild($this->createElement("related_commands"));

        $comandos = explode(",",$relatedcommand);
        foreach ($comandos as $c)
        {
            $casxml_command = $casxml_relatecommand->appendChild($this->createElement("command",$c));
        }

        $casxml_try = $item->appendChild($this->createElement("try_yourself"));
        $casxml_template = $casxml_try->appendChild($this->createElement("template",$template));
        $casxml_remarktry = $casxml_try->appendChild($this->createElement("remark",$remarktemplate));


        $casxml_exercises = $item->appendChild($this->createElement("exercises"));
        $casxml_exercise = $casxml_exercises->appendChild($this->createElement("exercise"));
        $casxml_descex = $casxml_exercise->appendChild($this->createElement("description",$exercisedescription));
        $casxml_hint = $casxml_exercise->appendChild($this->createElement("hint",$hint));
        $casxml_solut = $casxml_exercise->appendChild($this->createElement("solution",$solution));

        $casxml_filhelp = $item->appendChild($this->createElement("filehelp",$filehelp));
        if ($topico != "nada" && !empty($subtopico))
        {
            $item->getElementsByTagname("filehelp")->item(0)->nodeValue="$topico/$subtopico";
        }
    }

    public function alterItem( $name, $folder, $item, $description,
                            $sentence, $remarksentence, $remarkexample,
                            $relatedcommand,$template,$remarktemplate,
                            $exercisedescription,$hint,$solution,$filehelp, $topico = "nada", $subtopico = ''  )
    {
        $casxml_name = $item->setAttribute("name",$name);
        $casxml_desc = $item->getElementsByTagname("description")->item(0)->nodeValue = $description;

        $casxml_examples = $item->getElementsByTagname("examples")->item(0);
        $casxml_example = $casxml_examples->getElementsByTagname("example")->item(0);

        while ($casxml_example->hasChildNodes())
            $casxml_example->removeChild($casxml_example->childNodes->item(0));

        $comandos = explode("\r\n",$sentence);
        foreach ($comandos as $c)
        {
            $casxml_commandline = $casxml_example->appendChild($this->createElement("commandline"));
                $casxml_sentence = $casxml_commandline->appendChild($this->createElement("sentence",$c));
                $casxml_remark = $casxml_commandline->appendChild($this->createElement("remark"));
        }

        $casxml_example->appendChild($this->createElement("remark",$remarkexample));
        $casxml_relcom = $item->getElementsByTagname("related_commands")->item(0);
        while ($casxml_relcom->hasChildNodes())
            $casxml_relcom->removeChild($casxml_relcom->childNodes->item(0));

        $relcoms = explode(",",$relatedcommand);
        foreach ($relcoms as $r)
        {
            $casxml_command = $casxml_relcom->appendChild($this->createElement("command",$r));
        }

        $casxml_try = $item->getElementsByTagname("try_yourself")->item(0);
        $casxml_try->getElementsByTagname("template")->item(0)->nodeValue = $template;
        $casxml_try->getElementsByTagname("remark")->item(0)->nodeValue = $remarktemplate;

        $casxml_exercises = $item->getElementsByTagname("exercises")->item(0);
        $casxml_exercise = $casxml_exercises->getElementsByTagname("exercise")->item(0);
        $casxml_exercise->getElementsByTagname("description")->item(0)->nodeValue = $exercisedescription;
        $casxml_exercise->getElementsByTagname("hint")->item(0)->nodeValue = $hint;
        $casxml_exercise->getElementsByTagname("solution")->item(0)->nodeValue = $solution;

        /*if (!empty($filehelp))
        {
            $fh = $item->getElementsByTagname("filehelp")->item(0)->nodeValue;
            if (!empty($fh))
            {
                unlink("./bibliotecas/cas_filehelp/".$fh);
            }
            $item->getElementsByTagname("filehelp")->item(0)->nodeValue=$filehelp;
        }*/
        $item->getElementsByTagname("filehelp")->item(0)->nodeValue=$filehelp;
        if ($topico != "nada" && !empty($subtopico))
        {
            $item->getElementsByTagname("filehelp")->item(0)->nodeValue="$topico/$subtopico";
        }
    }


}

?>